# -*- coding: utf-8 -*-
"""
@Time ： 2022/12/27 1:24
@Auth ： jiraWu
@File ：_mysql_db.py
@IDE ：PyCharm
@Motto:人生苦短，我学Python

"""
from typing import Any

from pymysql.cursors import Cursor

"""
封装数据库操作类
"""
from pymysql import Connection


class DB:

    def __init__(self, user: str = 'root', password: str = 'root', host: str = 'localhost', port=3306,
                 autocommit: bool = True, charset: str = 'utf8mb4',):
        self.user = user
        self.password = password
        self.host = host
        self.port = port
        self.autocommit = autocommit
        self.charset = charset

    def conn(self, db: str):
        """
        连接数据库
        :param db: 数据库
        :return: 连接对象
        """
        conn = Connection(
            host=self.host,
            port=self.port,
            user=self.user,
            password=self.password,
            autocommit=self.autocommit,
            charset=self.charset,
            database=db
        )
        return conn

    def __str__(self):
        return f"连接【{self.host}:{self.port}】,用户【{self.user}】"


if __name__ == '__main__':
    conn = DB().conn('pymysql')
    cursor = conn.cursor()
    cursor.execute('select * from orders')
    res: tuple = cursor.fetchall()
    for row in res:
        print(row[0])
